package com.hakim.wfb.platform.controller;

import com.hakim.wfb.customer.params.consume.GetTodayConsumeInterestParam;
import com.hakim.wfb.customer.params.trans.*;
import com.hakim.wfb.customer.params.user.GetUserInfoParameter;
import com.hakim.wfb.customer.result.consume.ConsumeInterestView;
import com.hakim.wfb.customer.result.trans.*;
import com.hakim.wfb.customer.result.user.LoginUserView;
import com.hakim.wfb.customer.service.*;
import com.hkfs.fundamental.api.data.ListResultEx;
import com.hkfs.fundamental.api.data.ObjectResultEx;
import com.hkfs.fundamental.api.data.ResultEx;
import com.hkfs.fundamental.api.params.SessionIdentity;
import com.hkfs.fundamental.session.SessionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2017/7/5.
 */
@RequestMapping(value = "/transaction", name = "业务")
@RestController
public class TransactionController {

    @Autowired
    private TransactionService transactionService;

    @Autowired
    private CreditPointService creditPointService;

    @Autowired
    private PointOrderService pointOrderService;

    @Autowired
    private ConsumeRecordService consumeRecordService;

    @Autowired
    private WithdrawService withdrawService;

    @Autowired
    private ConsumeInterestService consumeInterestService;

    @RequestMapping(value = "/getUser", name = "用户信息", method = RequestMethod.POST)
    public ObjectResultEx<LoginUserView> getUser(GetUserInfoParameter param, HttpServletRequest request) {
        LoginUserView view = new LoginUserView();
        SessionHandler handler = new SessionHandler();
        SessionIdentity identity = handler.getSessionIdentity(request, SessionIdentity.class);
        view.setUserName(identity.getName());
        ObjectResultEx<LoginUserView> resultEx = new ObjectResultEx<>();
        resultEx.setDataObject(view);
        resultEx.makeSuccessResult();
        return resultEx;
    }

    @RequestMapping(value = "/getPoint", name = "获取积分", method = RequestMethod.POST)
    public ObjectResultEx<CreditPointView> getCreditPoint(GetCreditPointParameter parameter, HttpServletRequest request) {
        SessionHandler sessionHandler = new SessionHandler();
        SessionIdentity sessionIdentity = sessionHandler.getSessionIdentity(request, SessionIdentity.class);
        String userId = sessionIdentity.getUuid();
        parameter.setUserId(userId);
        return creditPointService.getCreditPoint(userId);
    }

    @RequestMapping(value = "/invest", name = "投资", method = RequestMethod.POST)
    public ResultEx invest(PlaceOrderParameter param, HttpServletRequest request) {
        SessionHandler handler = new SessionHandler();
        SessionIdentity identity = handler.getSessionIdentity(request, SessionIdentity.class);
        param.setSessionIdentity(identity);
        param.setUserId(identity.getUuid());
        return transactionService.placeOrder(param);
    }

    @RequestMapping(value = "/getPoints", name = "积分信息", method = RequestMethod.POST)
    public ListResultEx<PointOrderView> getPoints(GetPointOrderParameter param, HttpServletRequest request) {

        SessionHandler handler = new SessionHandler();
        SessionIdentity identity = handler.getSessionIdentity(request, SessionIdentity.class);
        param.setUserId(identity.getUuid());
        param.setSessionIdentity(identity);
        ListResultEx<PointOrderView> resultEx = pointOrderService.getPointOrders(param);
        return resultEx;
    }

    @RequestMapping(value = "/getProfitPoints", name = "分红", method = RequestMethod.POST)
    public ListResultEx<PointOrderView> getProfitPoints(GetProfitPointOrderParameter param, HttpServletRequest request) {

        SessionHandler handler = new SessionHandler();
        SessionIdentity identity = handler.getSessionIdentity(request, SessionIdentity.class);
        param.setUserId(identity.getUuid());
        param.setSessionIdentity(identity);
        ListResultEx<PointOrderView> resultEx = pointOrderService.getProfitPointOrders(param);
        return resultEx;
    }

    @RequestMapping(value = "/getFinished", name = "投资记录", method = RequestMethod.POST)
    public ListResultEx<ConsumeRecordView> getFinished(GetConsumeRecordParameter param, HttpServletRequest request) {

        SessionHandler handler = new SessionHandler();
        SessionIdentity identity = handler.getSessionIdentity(request, SessionIdentity.class);
        param.setUserId(identity.getUuid());
        param.setSessionIdentity(identity);
        ListResultEx<ConsumeRecordView> resultEx = consumeRecordService.getFinishedConsumeRecords(param);
        return resultEx;
    }

    @RequestMapping(value = "/getRunning", name = "投资记录", method = RequestMethod.POST)
    public ListResultEx<ConsumeRecordView> getRunning(GetConsumeRecordParameter param, HttpServletRequest request) {

        SessionHandler handler = new SessionHandler();
        SessionIdentity identity = handler.getSessionIdentity(request, SessionIdentity.class);
        param.setUserId(identity.getUuid());
        param.setSessionIdentity(identity);
        ListResultEx<ConsumeRecordView> resultEx = consumeRecordService.getRunningConsumeRecords(param);
        return resultEx;
    }

    @RequestMapping(value = "/getWithdraws", name = "提现记录", method = RequestMethod.POST)
    public ListResultEx<WithdrawRecordView> getWithdraws(GetWithdrawRecordParameter param, HttpServletRequest request) {

        SessionHandler handler = new SessionHandler();
        SessionIdentity identity = handler.getSessionIdentity(request, SessionIdentity.class);
        param.setUserId(identity.getUuid());
        param.setSessionIdentity(identity);
        ListResultEx<WithdrawRecordView> resultEx = withdrawService.getWithdrawRecords(param);
        return resultEx;
    }

    @RequestMapping(value = "/getTypes", name = "提现地址类型列表", method = RequestMethod.POST)
    public ListResultEx<WithdrawAddrTypeView> getTypes(HttpServletRequest request) {

        SessionHandler handler = new SessionHandler();
        SessionIdentity identity = handler.getSessionIdentity(request, SessionIdentity.class);
        ListResultEx<WithdrawAddrTypeView> resultEx = withdrawService.getTypes();
        return resultEx;
    }

    @RequestMapping(value = "/getAddrs", name = "提现地址列表", method = RequestMethod.POST)
    public ListResultEx<WithdrawAddrView> getAddrs(HttpServletRequest request) {

        SessionHandler handler = new SessionHandler();
        SessionIdentity identity = handler.getSessionIdentity(request, SessionIdentity.class);

        ListResultEx<WithdrawAddrView> resultEx = withdrawService.getWithdrawAddrs(identity.getUuid());
        return resultEx;
    }

    @RequestMapping(value = "/withdrawbind", name = "提现绑定", method = RequestMethod.POST)
    public ResultEx withdrawbind(GetWithdrawAddrParameter param, HttpServletRequest request) {
        SessionHandler handler = new SessionHandler();
        SessionIdentity identity = handler.getSessionIdentity(request, SessionIdentity.class);
        param.setUserId(identity.getUuid());
        return withdrawService.updateAddr(param);
    }

    /**
     * 提现
     *
     * @param param
     * @param request
     * @return
     */
    @RequestMapping(value = "/withdraw", name = "提现", method = RequestMethod.POST)
    public ResultEx withdraw(WithdrawCreditPointParameter param, HttpServletRequest request) {
        SessionHandler handler = new SessionHandler();
        SessionIdentity identity = handler.getSessionIdentity(request, SessionIdentity.class);
        param.setSessionIdentity(identity);
        return withdrawService.createWithdrawOrder(param);
    }

    /**
     * 积分提现计算
     *
     * @param param
     * @param request
     * @return
     */
    @RequestMapping(value = "/calculateWithdrawPoint", name = "积分提现计算", method = RequestMethod.POST)
    public ObjectResultEx<PointWithdrawCalculateView> calculateWithdrawPoint(CalculatePointWithdrawParameter param, HttpServletRequest request) {
        SessionHandler handler = new SessionHandler();
        SessionIdentity identity = handler.getSessionIdentity(request, SessionIdentity.class);
        param.setSessionIdentity(identity);
        return withdrawService.calculateWithdrawPoint(param);
    }

    @RequestMapping(value = "/getTodayInterest", name = "日息信息", method = RequestMethod.POST)
    public ObjectResultEx<ConsumeInterestView> getTodayInterest(GetTodayConsumeInterestParam param, HttpServletRequest request) {

        ObjectResultEx<ConsumeInterestView> resultEx = consumeInterestService.getTodayInterest(param);
        return resultEx;
    }

    /**
     * 转账
     *
     * @param param
     * @param request
     * @return
     */
    @RequestMapping(value = "/transPoint", name = "转账", method = RequestMethod.POST)
    public ResultEx transPoint(GetTransPointParameter param, HttpServletRequest request) {
        SessionHandler handler = new SessionHandler();
        SessionIdentity identity = handler.getSessionIdentity(request, SessionIdentity.class);
        param.setSessionIdentity(identity);
        return transactionService.createTransPointOrder(param);
    }

    /**
     * 积分兑换计算
     *
     * @param param
     * @param request
     * @return
     */
    @RequestMapping(value = "/calculatePoint", name = "积分兑换计算", method = RequestMethod.POST)
    public ObjectResultEx<PointExchangeCalculateView> calculatePoint(CalculatePointExchangeParameter param, HttpServletRequest request) {
        SessionHandler handler = new SessionHandler();
        SessionIdentity identity = handler.getSessionIdentity(request, SessionIdentity.class);
        param.setSessionIdentity(identity);
        return transactionService.calculateExchangePoint(param);
    }

    /**
     * 积分兑换
     *
     * @param param
     * @param request
     * @return
     */
    @RequestMapping(value = "/exchangePoint", name = "积分兑换", method = RequestMethod.POST)
    public ResultEx exchangePoint(ExchangeCreditPointParameter param, HttpServletRequest request) {
        SessionHandler handler = new SessionHandler();
        SessionIdentity identity = handler.getSessionIdentity(request, SessionIdentity.class);
        param.setSessionIdentity(identity);
        return transactionService.exchangePoint(param);
    }
}
